$(function(){	
	PageBackgroundPosition();
	UserPanelPosition();
	$(window).resize(PageBackgroundPosition);
	$(window).scroll(function(){UserPanelPosition(true)});
	
	$("#control-panel").click(function(){
		var l = parseInt($(this).css("left"));
		if(l<0){
			//show
			$(this).animate({
				left: 0
			});
		}else{
			//hide
			$(this).animate({
				left: -210
			});
		}
	});
	
	$("input.empty-text").each(function(){
		var title = $(this).attr("title");
		if(title){
			if($(this).val().length == 0){
				$(this).val(title).addClass("empty-text");
			}
			
			$(this).focus(function(){
				if($(this).val()==title){
					$(this).val("").removeClass("empty-text");
				}
			});
			$(this).blur(function(){
				if($(this).val()==""){
					$(this).val(title).addClass("empty-text");
				}
			});
		}
	});
});
function PageBackgroundPosition(){
	var bottom = $(".page-bg.bottom");
	var top = $(".page-bg.top");
	var site = $("#site");
	var l = ($(window).width()-site.width())/2;
	
	site.css("left", l>0 ? l : 0);
	bottom.css("top", site.height() - 50);
	top.css("height", site.height() - 50 + bottom.height());
}
function UserPanelPosition(animate){
	animate = animate ? animate : false;
	var panel = $("#control-panel");
	pos = ($(window).height()-panel.height())/2 + $(window).scrollTop();
	scrolling=true;
	if(animate){
		scrolling=false;
		setTimeout(function(){
			if(!scrolling){
				panel.animate({top: pos}, 700, "easeOutQuart");
			}
		}, 200);
	}else{
		panel.css("top", pos);
	}
}

function PageEdit(page){
	$.ajax({
		url: "/admin/page_edit/",
		data: {page: page},
		type: "post",
		beforeSend: function(){
			$(".throbber").show();
		},
		complete: function(req){
			$(".throbber").hide();
			if(req.status==200){
				var div = $("<div />");
				div.html(req.responseText);
				
				var title = $("#menu .selected strong").html();
				title = title ? title : "Index";
				
				//Edit Dialog
				div.dialog({
					width:800,
					height:600,
					modal:true,
					title:title,
					resizable:false,
					beforeClose:function(){
						div.remove();
						$('.image-picker').remove();
					}
				});
				
				//WYSIWYG editor
				editor = $(".editor").wysiwyg({
					css: "/stylesheet/style_editor.css",
					controls: {
						insertImage: {
							visible: true,
							exec: function(){
								ImagePick(this);
							}
						}
					}
				});
				
				//Tabs
				Tabs();
			}
		}
	});
}
function PageSave(form){
	form = $(form);
	var sbmt = form.find("input[type=submit]");
	var msg = form.find(".message");
	
	$.ajax({
		url: "/admin/page_save",
		data: form.serialize(),
		type: "post",
		beforeSend: function(){
			$(".throbber").show();
			sbmt.attr("disabled","disabled");
		},
		complete: function(req){
			$(".throbber").hide();
			sbmt.removeAttr("disabled");
			if(req.status==200){
				var className = parseInt(req.getResponseHeader("X-Status")) > 0 ? "ok" : "error";
				msg.removeClass("ok").removeClass("error").addClass(className);
				msg.html(req.responseText).fadeIn();
				setTimeout(function(){
					msg.fadeOut();
				}, 2000);
			}
		}
	});
}

function Tabs(){
	$("ul.tabs").each(function(){
		if(!$(this).hasClass("inited")){
			$(this).addClass("inited");
			
			$(this).find("li a").click(function(){
				var id = $(this).attr("href").replace(/^#/, "");
				var ul = $(this).parent().parent();
				var container = ul.parent();
				
				ul.find("li").removeClass("selected");
				$(this).parent().addClass("selected");
				container.find(".tab-content").hide();
				container.find("#tab-content-"+id).show();
			});
			
			var first = $(this).find("li").eq(0);
			first.addClass("selected");
			$("#tab-content-"+first.find("a").attr("href").replace(/^#/,"")).show();
		}
	});
}